/**
  * 充值
  */
module game {

    export class AddMoneyMediator extends BaseMediator {
        public static NAME: string = "AddMoneyMediator";
        public constructor(viewComponent: any = null) {
            super(AddMoneyMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_ADD_MONEY,
                PanelNotify.HALL.CLOSE_ADD_MONEY
            ];
        }
        public handleNotification(notification: puremvc.INotification): void {
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_ADD_MONEY: {
                    this.panel = null;
                    this.panel = new AddMoneyPanel();
                    this.showUI(this.panel, 2, 1, true, false);
                    break;
                }
                case PanelNotify.HALL.CLOSE_ADD_MONEY: {
                    this.closePanel(0);
                    break;
                }
            }
        }
        private panel: AddMoneyPanel;
        public obj = null;
        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this)
            this.panel.chongzhijilv.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
                game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_ADD_MONEY_JILU)
            }, this)
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            var time = Global.getCurrTimeLong()
            var signature = Global.getSynLoginKey(time)
            signature = signature.toUpperCase()
            var request = new egret.HttpRequest();
            request.responseType = egret.HttpResponseType.TEXT;
            request.open(InstructionsEnum.IP_API + "/Payment/List?userid=" + GameVO.getVo().dwUserID + "&signature=" + signature + "&time=" + time, egret.HttpMethod.GET);
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.addEventListener(egret.Event.COMPLETE, (event: egret.Event) => {
                EffectUtils.hideWaritPanel()
                try {
                    var request = <egret.HttpRequest>event.currentTarget;
                    var obj = JSON.parse(request.response)
                    this.showPalyType(obj)
                } catch (err) {
                    EffectUtils.textFadeAway("数据包异常，无法解析!")
                }
            }, this);
            EffectUtils.showWaritPanel()
            request.send();
            //根据后端查询回来的支付方式列表。来展示对应的支付方式按钮，最后把排在第一的按钮自动点击一下 弹出右方的内容框体
        }
        private showPalyType(obj: Object) {
            if (obj["code"] == 101) {
                Global.tipShow(obj["msg"])
                return;
            }
            this.obj = obj;
            console.log(obj);
            this.panel.palyTypeGroup.removeChildren();
            var Wxs: Array<any> = obj["Wxs"]; //QQ充值渠道
            var OfflinePay: Array<any> = obj["OfflinePay"];//线下支付渠道
            var PayServerInfoCells: Array<any> = obj["PayServerInfoCells"];//线上支付渠道
            var isOpen = false;
            //先检查是否开启QQ充值
            if (Wxs.length != 0) {
                var button = new eui.Button();
                button.skinName = skins.ButtonSkin_play;
                button.touchEnabled = true;
                button.icon = "chongzhi_qq_h";
                button.label = "VIP充值"
                button.addEventListener(egret.TouchEvent.TOUCH_TAP, this.WxsClick, this);
                this.panel.palyTypeGroup.addChild(button);
                //自动打开QQ充值界面
                button.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
                isOpen = true;
            }
            //再检查是否开启线下支付
            for (let i = 0; i < OfflinePay.length; i++) {
                let obj = OfflinePay[i];
                var PayName = obj["PayName"];
                var PayType = obj["PayType"];
                var list: Array<Object> = obj["list"];
                var TransferType = list[0]["TransferType"];
                var button = new eui.Button();
                button.skinName = skins.ButtonSkin_play;
                button.touchEnabled = true;
                button.label = PayName + ""
                button.name = PayType + "_" + TransferType;
                if (PayType == "OfflineBankPay") {//银联
                    button.icon = "chongzhi_yl_h";
                } else if (PayType == "OfflineWeiXin") {//微信
                    button.icon = "chongzhi_wx_h";
                } else if (PayType == "OfflineZhiFuBao") {//支付宝
                    button.icon = "chongzhi_zfb_h";
                }
                button.addEventListener(egret.TouchEvent.TOUCH_TAP, this.OfflinePayClick, this);
                this.panel.palyTypeGroup.addChild(button);
                //如果没有QQ充值，自动打开第一个线下支付
                if (i == 0 && !isOpen) {
                    button.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
                    isOpen = true;
                }
            }

            //最后检查是否开启线上支付
            for (let i = 0; i < PayServerInfoCells.length; i++) {
                let obj = PayServerInfoCells[i];
                var PayId = obj["PayId"];
                var PayName = obj["PayName"];
                var PayType = obj["PayType"]

                var button = new eui.Button();
                button.skinName = skins.ButtonSkin_play;
                button.touchEnabled = true;
                button.label = PayName + ""
                button.name = PayId + "";
                if (PayType == "JingDong") {//京东
                    button.icon = "chongzhi_jd_h";
                } else if (PayType == "UnionPay") {//云闪付
                    button.icon = "chongzhi_ysf_h";
                } else if (PayType == "QuickPay") {//银联
                    button.icon = "chongzhi_yl_h";
                } else if (PayType == "WeiXin") {//微信
                    button.icon = "chongzhi_wx_h";
                } else if (PayType == "ZhiFuBao") {//支付宝
                    button.icon = "chongzhi_zfb_h";
                }
                button.addEventListener(egret.TouchEvent.TOUCH_TAP, this.PayServerInfoCellsClick, this);
                this.panel.palyTypeGroup.addChild(button);
                //如果没有QQ充值和线下，自动打开第一个线上支付
                if (i == 0 && !isOpen) {
                    button.dispatchEventWith(egret.TouchEvent.TOUCH_TAP);
                    isOpen = true;
                }
            }

        }

        //点击QQ支付方式按钮
        private WxsClick(eve: egret.TouchEvent) {
            this.ShowClickButton(eve)
            this.panel.vive.removeChildren();
            var vive = new game.CZ_VIPchongzhiPanel(this.obj);
            this.panel.vive.addChild(vive)
        }
        //点击线下支付方式按钮
        private OfflinePayClick(eve: egret.TouchEvent) {
            this.ShowClickButton(eve)
            this.panel.vive.removeChildren();
            var button = <eui.Button>eve.currentTarget;
            var OfflinePay: Array<Object> = this.obj["OfflinePay"]
            for (let i = 0; i < OfflinePay.length; i++) {
                let obj = OfflinePay[i];
                var PayType = obj["PayType"]
                var list: Array<Object> = obj["list"];
                var TransferType = list[0]["TransferType"]
                if (button.name == PayType + "_" + TransferType) {
                    var vive = null;
                    if (TransferType == 2) {
                        vive = new game.CZ_XianXiaSaoMaPanel(obj);
                    } else {
                        vive = new game.CZ_XianXiaPanel(obj);
                    }
                    this.panel.vive.addChild(vive)
                    break;
                }
            }
        }

        //点击线上支付方式按钮
        private PayServerInfoCellsClick(eve: egret.TouchEvent) {
            this.ShowClickButton(eve)
            this.panel.vive.removeChildren();
            var button = <eui.Button>eve.currentTarget;
            var PayServerInfoCells: Array<Object> = this.obj["PayServerInfoCells"]
            for (let i = 0; i < PayServerInfoCells.length; i++) {
                let obj = PayServerInfoCells[i];
                if (button.name == obj["PayId"]) {
                    var vive = new game.CZ_XianShangPanel(obj);
                    this.panel.vive.addChild(vive)
                    break;
                }
            }
        }
        //点击左侧支付方式按钮，点击的高亮，其他的置灰
        private ShowClickButton(eve: egret.TouchEvent) {
            let leng = this.panel.palyTypeGroup.numChildren
            for (let i = 0; i < leng; i++) {
                if (this.panel.palyTypeGroup.getChildAt(i) instanceof eui.Button) {
                    let button: eui.Button = <eui.Button>this.panel.palyTypeGroup.getChildAt(i);
                    let icon: string = button.icon + "";
                    if (icon.indexOf("_h") == -1) {
                        button.icon = icon + "_h"
                    }
                }
            }
            let button = <eui.Button>eve.currentTarget;
            let icon: string = button.icon + "";
            button.icon = icon.replace("_h", "")
        }

        private closeButtonClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }

    }
}
